package oa.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import oa.entity.Attendance;
import oa.entity.User;
import oa.paging.Page;
import oa.service.CompanyService;
import oa.service.impl.AttendanceServiceImpl;
import oa.service.impl.CompanyServiceImpl;
import oa.service.impl.DepartmentServiceImpl;
import oa.service.impl.UserServiceImpl;

/**
 * Servlet implementation class UserattenDanceList
 */
@WebServlet("/userattendancelist")
public class UserattenDanceList extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UserattenDanceList() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		HttpSession session = request.getSession();
		PrintWriter out = response.getWriter();
		DepartmentServiceImpl deptimpl = new DepartmentServiceImpl(); // 部门
		AttendanceServiceImpl attendan = new AttendanceServiceImpl(); // 考勤
		CompanyService company = new CompanyServiceImpl(); // 公司
		UserServiceImpl user = new UserServiceImpl(); //员工userid
		String opt = request.getParameter("opt");
		int userid = 0;
	    String pageNo  = request.getParameter("PageNo");
		if(pageNo==null) {
			pageNo ="1";
		}
		if("useridshow".equals(opt)) {
			userid = Integer.parseInt(request.getParameter("userid"));
		}else {
			userid=(int)session.getAttribute("userid");
		}
		try {			
		    int count = attendan.SecletCountSum(userid);//表示该员工的数据总数量	    
		    Page<Attendance> pageInfo = new Page<Attendance>();	    		   	   
		    pageInfo.setCurrentPage(Integer.parseInt(pageNo));
		    pageInfo.setPageSize(5);
		    pageInfo.setTotalRow(count);
		    List<Attendance> AttendanceList=attendan.selectFenYe(userid,Integer.parseInt(pageNo), pageInfo.getPageSize());
			pageInfo.setPageList(AttendanceList);
			session.setAttribute("CompanyList", company.selectAllCompany(0));
			session.setAttribute("DeptimplList", deptimpl.selectAllDepartment(0,0, 0));
			session.setAttribute("UserIdList",user.selectAllUser(null,0,0));   
			session.setAttribute("pageInfo",pageInfo);		
            request.getRequestDispatcher("user_attendancelist.jsp").forward(request, response);
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
